package com.xtravel.common.helper;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import com.google.gson.Gson;
import org.apache.commons.collections.FastHashMap;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@SuppressLint("NewApi")
public class ConvertUtils {

    /**
     * Convert ResourceBundle into a Map object.
     *
     * @param resource a resource bundle to convert.
     * @return Map a map version of the resource bundle.
     */
    public static Map<String, Object> convertBundleToMap(Bundle bundle) {
        Map<String, Object> map = new HashMap<String, Object>();

        Set<String> keys = bundle.keySet();
        for (String key : keys) {
            map.put(key, bundle.get(key));
        }

        return map;
    }

    @SuppressLint("DefaultLocale")
    public static ContentValues convertBeanToContentValues(Object bean) {

        ContentValues cv = new ContentValues();
        Field[] fields = LangUtil.getFields(bean.getClass());
        for (int i = 0, len = fields.length; i < len; i++) {
            Field f = fields[i];
            String fieldName = f.getName();
            Method getMethod = null;

            try {
                getMethod = LangUtil.getGetter(bean.getClass(), fieldName);
                if (getMethod == null) {
                    continue;
                }
                String returntype = getSqliteTypeClass(f.getType()).getSimpleName().toLowerCase();
                Object val = getMethod.invoke(bean);
                // Log.d(TAG,returntype+":"+fieldName+":"+val);
                if (val == null) {
                    continue;
                }
                if (returntype.equals("string")) {
                    cv.put(fieldName, (String) val);
                } else if (returntype.equals("character")) {
                    cv.put(fieldName, val.toString());
                } else if (returntype.equals("boolean")) {
                    cv.put(fieldName, (Boolean) val);
                } else if (returntype.equals("integer")) {
                    cv.put(fieldName, (Integer) val);
                } else if (returntype.equals("byte")) {
                    cv.put(fieldName, (Byte) val);
                } else if (returntype.equals("short")) {
                    cv.put(fieldName, (Short) val);
                } else if (returntype.equals("double")) {
                    cv.put(fieldName, (Double) val);
                } else if (returntype.equals("float")) {
                    cv.put(fieldName, (Float) val);
                } else if (returntype.equals("long")) {
                    cv.put(fieldName, (Long) val);
                } else {
                    cv.putNull(fieldName);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return cv;
    }

    private static Class<?> getSqliteTypeClass(Class<?> classz) {
        return classz.isPrimitive() ? LangUtil.getWrapperClass(classz) : classz;
    }

    public static FastHashMap convertCursorToMap(Cursor cursor) {
        FastHashMap res = new FastHashMap();

        String[] columnNames = cursor.getColumnNames();
        for (String columnName : columnNames) {
            int columnIndex = cursor.getColumnIndex(columnName);
            switch (cursor.getType(columnIndex)) {
                case Cursor.FIELD_TYPE_FLOAT:
                    res.put(columnName, cursor.getFloat(columnIndex));
                    break;
                case Cursor.FIELD_TYPE_INTEGER:
                    res.put(columnName, cursor.getInt(columnIndex));
                    break;
                case Cursor.FIELD_TYPE_STRING:
                    res.put(columnName, cursor.getString(columnIndex));
                    break;
                case Cursor.FIELD_TYPE_BLOB:
                    res.put(columnName, cursor.getBlob(columnIndex));
                    break;
                case Cursor.FIELD_TYPE_NULL:
                default:
                    res.put(columnName, cursor.getString(columnIndex));
                    break;
            }

        }

        return res;
    }

    public static <T> T populate(Class<T> cls, FastHashMap map) {
        Gson gson = new Gson();
        String s = gson.toJson(map);

        return (T) gson.fromJson(s, cls);
//        for (Object key : map.keySet()) {
//            Field field = LangUtil.getField(item.getClass(), key.toString());
//            if (field != null) {
//                String keyStr = key.toString();
//                Method method = LangUtil.getSetter(item.getClass(), keyStr, field.getType());
//                if (method != null) {
//                    method.invoke(item, map.get(key));
//                }
//            }
//        }
    }

    public static int dip2px(Context context, int dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public static int px2dip(Context context, int px) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }
}
